package pers.cnzdy.tutorial.Chapter7_Data.Section3_SQLite;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import pers.cnzdy.tutorial.R;

public class SQLiteActivity extends AppCompatActivity {
    private DBQuizHelper dbQuizHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);

        dbQuizHelper = new DBQuizHelper(this, "Exam.db", null, 2);
        Button btnCreateDB = findViewById(R.id.create_database);
        btnCreateDB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbQuizHelper.getWritableDatabase();
            }
        });

        Button btnAddDate = findViewById(R.id.add_data);
        btnAddDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbQuizHelper.getWritableDatabase();
                ContentValues values = new ContentValues();

                values.put("statement", "简述Android中Intent的作用。");
                values.put("type", "简答题");
                values.put("answer", "Intent用于同一应用或不同应用的组件之间通信。");
                values.put("difficulty", "2");
                db.insert("Quiz", null, values);
                values.clear();

                values.put("statement", "Service结束运行的方法有哪两种？有何区别？");
                values.put("type", "简答题");
                values.put("answer", "Service 启动后将一直处于运行状态，" +
                        "即使调用它的进程结束了，Service 仍然存在，" +
                        "直到有进程调用 stopService()，或者 Service " +
                        "自己调用stopSelf() 才会结束运行");
                values.put("difficulty", "3");
                db.insert("Quiz", null, values);
            }
        });

        Button btnUpdateData = findViewById(R.id.update_data);
        btnUpdateData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbQuizHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("difficulty", "4");
                db.update("Quiz", values, "statement = ?",
                        new String[]{"Service结束运行的方法有哪两种？有何区别？"});
            }
        });

        Button btnDelete = findViewById(R.id.delete_data);
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbQuizHelper.getWritableDatabase();
                db.delete("Quiz", "difficulty > ?", new String[]{"4"});
            }
        });

        Button queryButton = findViewById(R.id.query_data);
        queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbQuizHelper.getWritableDatabase();

                Cursor cursor = db.query("Quiz", null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    do {
                        String statement = cursor.getString(cursor.
                                getColumnIndex("statement"));
                        String type = cursor.getString(cursor.
                                getColumnIndex("type"));
                        int difficult = cursor.getInt(
                                cursor.getColumnIndex("difficulty"));

                        Log.d("SQLiteActivity", "试题: " + statement +
                                "题型： " + type + "难度：" + difficult);
                    } while (cursor.moveToNext());
                }
                cursor.close();
            }
        });
    }

}